//
//  UploadImageManager.swift
//  IndustryPark
//
//  Created by Samuel on 2017/4/14.
//  Copyright © 2017年 RedSoft. All rights reserved.
//

import Foundation
import Alamofire
import Qiniu
import SwiftyJSON

public typealias UploadSuccess = () -> ()
public typealias UploadFailure = () -> ()

class UploadImageManager: NSObject {

    class func upload(_ image:UIImage, success:@escaping UploadSuccess, failure:@escaping UploadFailure ) {
        Alamofire.request("\(BASE_URL)qiniuToken", method:.get ).responseJSON { response in
            switch response.result.isSuccess{
            case true:
                if let value = response.result.value {
                    let json = JSON(value)
                    if let token = json["data"]["qiniuToken"].string{
                        let uploader = QNUploadManager.init()
                        let data = UIImageJPEGRepresentation(image, 0.2)
                        uploader?.put(data!, key: nil, token: token, complete: { (info, key, resp) in
                            printLog(resp!)
                            if (info?.statusCode == 200){
                                if let dict = resp?["data"] as? NSDictionary{
                                    let key = dict["key"]
                                    image.qiniuKey = key as? String
                                }
                                success()
                            }else{
                                failure()
                            }
                        }, option: nil)
                    }
                }
                
            case false:
                failure()
                printLog(response.result.error)
            }
        }
    }
    
    class func uploadImages(_ images:[UIImage], success:@escaping UploadSuccess, failure:@escaping UploadFailure ) {
        if images.isEmpty {
            success()
        }
        Alamofire.request("\(BASE_URL)qiniuToken", method:.get ).responseJSON { response in
            switch response.result.isSuccess{
            case true:
                if let value = response.result.value {
                    let json = JSON(value)
                    if let token = json["data"].string{
                        var index = 0
                        var successCount = 0
                        let totalCount = images.count
                        
                        let queue = DispatchQueue.global()
                        
                        for image in images {
                            queue.async {
                                let uploader = QNUploadManager.init()
                                let data = UIImageJPEGRepresentation(image, 0.6)
                                uploader?.put(data!, key: nil, token: token, complete: { (info, key, resp) in
                                    index += 1
                                    if (info?.statusCode == 200){
                                        if let key = resp?["key"] {
                                            image.qiniuKey = key as? String
                                        }
                                        successCount += 1
                                    }
                                    if (index == totalCount){
                                        if (successCount == totalCount){
                                            success()
                                        }else{
                                            failure()
                                        }
                                    }
                                }, option: nil)
                            }
                        }
                    }
                }
                
            case false:
                failure()
                printLog(response.result.error)
            }
        }
    }
}
